#include "stdafx.h"
#include "ObjectUpdater.h"

VWD::ObjectUpdater::ObjectUpdater(void)
:updateStyle(CallLastOne)
{
}

VWD::ObjectUpdater::~ObjectUpdater(void)
{
}

bool VWD::ObjectUpdater::frameStarted( const Ogre::FrameEvent& evt )
{
	switch(updateStyle)
	{
	case CallEveryOne:
		for(UpdateVector::iterator i = objectsBeUpdated.begin();i!=objectsBeUpdated.end();i++)
		{
			(*i)->Update(evt.timeSinceLastFrame);
		}
		break;

	case CallLastOne:
		if(objectsBeUpdated.size()>0)
			if(!(*(objectsBeUpdated.end()-1))->Update(evt.timeSinceLastFrame))
				objectsBeUpdated.erase(objectsBeUpdated.end()-1);
		break;

	default:
		break;
	}

	return true;
}

void VWD::ObjectUpdater::AddObject( Behavior::IUpdateRequired* obj )
{
	objectsBeUpdated.push_back(obj);
}

void VWD::ObjectUpdater::RemoveObject( Behavior::IUpdateRequired* obj )
{
	objectsBeUpdated.erase(std::find(objectsBeUpdated.begin(),objectsBeUpdated.end(),obj));
}

VWD::ObjectUpdater::InvokeStyle VWD::ObjectUpdater::GetUpdateStyle() const
{
	return updateStyle;
}

void VWD::ObjectUpdater::SetUpdateStyle( InvokeStyle val )
{
	updateStyle = val;
}